package cn.hesion.leetcode;
/**
 * ClassName: FrequencySort <br/>
 * Description: <br/>
 * date: 2022/6/3 6:15<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import java.util.*;

/**
 * 1636. 按照频率将数组升序排序
 *
 * @author: hesion
 * @create: 2022-06-03 06:15
 **/
public class FrequencySort {
    public int[] frequencySort(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        ArrayList<Integer> arrToList = new ArrayList<>();
        for (int i = 0; i < nums.length; ++i) {
            arrToList.add(nums[i]);
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
        }

        arrToList.sort((a, b) -> {
            System.out.println(a);
            System.out.println(b);
            Integer aCount = map.getOrDefault(a, 0);
            Integer bCount = map.getOrDefault(b, 0);
            if (Objects.equals(aCount, bCount)) {
                return b - a;
            }
            return aCount-bCount;
        });

        return arrToList.stream().mapToInt(Integer::intValue).toArray();

    }
}
